#include<iostream>
#include<string>
#include<vector>
#include<fstream>
    static std::string UrlEncode(const std::string url,bool convert_space_to_plus){
        std::string res;
        for(auto& c:url){
            if(c=='.'||c=='-'||c=='_'||c=='~'||isalnum(c)){   //isalnum函数的作用是判断字符是否为数字或字母（大小写都包含）
                res+=c;
                continue;
            }
            if(c==' '&&convert_space_to_plus==true){
                res+='+';
                continue;
            }
            //剩下的都需要编码
            char tmp[4]={0};
            snprintf(tmp,4,"%%%02X",c);
            res+=tmp;
        }
        return res;
    }
    static char HEXTOI(char c){
        if(c>'0'&&c<'9'){
            return c-'0';
        }else if(c>='a'&&c<='z'){
            return c-'a'+10;
        }else if(c>='A'&&c<='Z'){
            return c-'A'+10;
        }
        return -1;
    }
    static std::string UrlDecode(const std::string& url,bool convert_space_to_plus){
        std::string res;
        for(int i=0;i<url.size();i++){
            if(url[i]=='+'&&convert_space_to_plus==true){
                res+=' ';
                continue;
            }
            if(url[i]=='%'&&(i+2)<url.size()){
                char v1=HEXTOI(url[i+1]);
                char v2=HEXTOI(url[i+2]);
                char v=v1*16+v2;
                res+=v;
                i+=2;
                continue;
            }
            res+=url[i];
        }
        return res;
    }
int main(){
    std::string buffer="/login?user=bite&passwd=C++";
    std::string res=UrlEncode(buffer,true);
    std::string ret=UrlDecode(res,true);
    std::cout<<ret<<std::endl;

    return 0;
}